package com.uyong.study.snake;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private Snake snake;
	private Food food;
	private Timer timer;
	private int score;// 分数
	private int length;// 蛇身长度
	private int speed;// 速度

	public GamePanel() {
		snake = new Snake();
		food = new Food();
		speed = 200;
		timer = new Timer(speed, new SnakeRunListerner());

		setSize(SizeConstants.WIDTH * SizeConstants.SIZE, SizeConstants.HEIGHT * SizeConstants.SIZE);
		setBorder(BorderFactory.createEtchedBorder(Color.RED, Color.RED));
		addKeyListener(new KeyControl());
	}

	// 开始游戏
	public void startGame() {
		score = 0;
		length = snake.getSnakeBody().size();
		timer.start();
	}

	// 暂停
	public void stop() {
		if (timer.isRunning()) {
			timer.stop();
		} else {
			timer.start();
		}
	}

	// 重新开始
	public void againStart() {
		repaint();
		snake.againStart();
		speed = 200;
		startGame();
	}

	// 当吃到食物后产生新食物
	public void eatFood() {
		if (snake.isEatFood(food)) {
			addSpeed();
			timer.setDelay(speed);
			countScore();
			countLength();
			snake.eatFood();
			snake.setColor(food.getColor());
			food.changeColor();
			food.createFood(snake);
		}
	}

	// 游戏结束后的选择
	public void gameEnd() {
		if (!snake.getLife()) {

			int option = JOptionPane.showConfirmDialog(null, "是否继续?", "游戏已结束", JOptionPane.YES_NO_OPTION);
			if (option == JOptionPane.YES_OPTION) {
				speed = 200;
				againStart();
			} else {
				System.exit(0);
			}
		}
	}

	// 速度增长比例
	public void addSpeed() {
		speed = speed - 5;
	}

	// 分数计算
	public void countScore() {
		score = score + 2 * (length - 2);
	}

	// 长度计算
	public void countLength() {
		length++;
	}

	public int getScore() {
		return score;
	}

	public int getLength() {
		return length;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(getBackground());
		g.fillRect(1, 1, SizeConstants.WIDTH * SizeConstants.SIZE, SizeConstants.HEIGHT * SizeConstants.SIZE);
		snake.drawSnake(g);
		food.drawFood(g);
		eatFood();
	}

	private class SnakeRunListerner implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == timer) {
				if (snake.getLife()) {
					snake.snakeMove();
					gameEnd();
					repaint();
				}
			}
		}
	}

	private class KeyControl extends KeyAdapter {
		@Override
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			switch (key) {
			case KeyEvent.VK_UP:
				snake.changeDeriction(snake.UP);
				break;
			case KeyEvent.VK_DOWN:
				snake.changeDeriction(snake.DOWN);
				break;
			case KeyEvent.VK_LEFT:
				snake.changeDeriction(snake.LEFT);
				break;
			case KeyEvent.VK_RIGHT:
				snake.changeDeriction(snake.RIGHT);
				break;
			}
		}
	}
}
